package com.t2t.top.service;

import com.t2t.top.dao.UserMapper;
import com.t2t.top.exception.ServiceException;
import com.t2t.top.model.po.User;
import com.t2t.top.model.po.UserExample;
import com.t2t.top.service.impl.UserServiceImpl;
import org.easymock.EasyMock;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

import java.util.Date;

/**
 * Created by yangpengfei on 2016/7/30.
 */
public class UserServiceImplEasyMock {

    Logger logger = LoggerFactory.getLogger(this.getClass());

    private UserServiceImpl userService = new UserServiceImpl();
    private UserMapper mapper = null;

    @BeforeMethod
    public void test_setup() throws ServiceException {
        mapper = EasyMock.createStrictMock(UserMapper.class);
        userService.userMapper = mapper;
        System.out.println("setup ...");
    }

    @Test
    public void test_delete() throws ServiceException {
        User user = new User();
        user.setId(1);
        user.setBirthday(new Date());
        user.setName("杨鹏飞");
        user.setSex(true);
        user.setPassword("123456789");
        user.setUserType("管理员");

        //1 创建UserServiceImpl对象，进入record阶段
        EasyMock.expect(mapper.selectByPrimaryKey(user.getId())).andReturn(user);
        EasyMock.expect(mapper.deleteByPrimaryKey(user.getId())).andReturn(1);

        //2 进入测试阶段
        EasyMock.replay(mapper);

        userService.delete(user);
        System.out.println("ok");

        //3 验证交互关系
        EasyMock.verify(mapper);

    }

    public void test_add() throws ServiceException {
        User user = new User();
        user.setBirthday(new Date());
        user.setName("杨鹏飞");
        user.setSex(true);
        user.setPassword("123456789");
        user.setUserType("管理员");

        //1 创建UserServiceImpl对象，进入record阶段
        UserExample e = new UserExample();
        e.createCriteria().andNameEqualTo(user.getName());
        EasyMock.expect(mapper.selectByName(user.getName())).andReturn(null);
        EasyMock.expect(mapper.insertSelective(user)).andReturn(1);

        //2 进入测试阶段
        EasyMock.replay(mapper);

        System.out.println(userService.add(user));

        //3 验证交互关系
        EasyMock.verify(mapper);
    }
}
